<template>
  <div>
    <el-form label-position="left" label-width="120px" style="width:100%;">
      <el-form-item label="车牌号："><el-input v-model="formData.plateNumber" placeholder="请输入车牌号" class="filter-item" /></el-form-item>
      <el-form-item label="优先次数："><el-input-number v-model="formData.discountNum" placeholder="请输入优惠次数" controls-position="right" class="filter-item" /></el-form-item>
      <el-form-item label="过期时间："><el-date-picker v-model="formData.expireTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择过期时间" :editable="false" class="filter-item" /></el-form-item>
      <el-form-item label="白名单原因：">
        <el-radio-group v-model="formData.reasonType">
          <el-radio-button label="1">订单原因</el-radio-button>
          <el-radio-button label="2">摄像头未拍到离场记录</el-radio-button>
          <el-radio-button label="3">其他</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="兑现状态：">
        <el-radio-group v-model="formData.exchange">
          <el-radio-button label="0">未兑现</el-radio-button>
          <el-radio-button label="1">已兑现</el-radio-button>
          <el-radio-button label="2">已过期</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="兑现时间："><el-date-picker v-model="formData.exchangeDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择兑现时间" :editable="false" class="filter-item" /></el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button icon="el-icon-close" @click="cancel">取消</el-button>
      <el-button v-waves :loading="confirmLoading" type="primary" icon="el-icon-check" @click="submit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getWhitelistDetail, addWhitelist, editWhitelist } from '@/api//whitelist';
import waves from '@/directive/waves';

export default {
  name: 'WhitelistAdd',
  directives: { waves },
  props: {
    id: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      confirmLoading: false,
      formData: {
        reasonType: null,
        exchange: null
      }
    };
  },
  created() {
    if (this.id) {
      this.getDetail(this.id);
    } else {
      this.formData.reasonType = 1;
      this.formData.exchange = 0;
    }
  },
  methods: {
    async getDetail(id) {
      await getWhitelistDetail(id).then(response => {
        this.formData = response.data;
      });
    },
    async submit() {
      if (this.id) {
        if (this.formData.plateNumber === '') {
          this.$message.error('请输入车牌号');
          return;
        }
      }
      this.confirmLoading = true;
      if (this.id) {
        await editWhitelist(this.formData).then(res => {
          this.confirmLoading = false;
          if (res.msg !== 'OK') {
            this.$message.error(res.msg);
            return;
          }
          this.$message.success('编辑成功');
          this.$emit('cancelClose', true, 'edit');
        });
      } else {
        await addWhitelist(this.formData).then(res => {
          this.confirmLoading = false;
          if (res.msg !== 'OK') {
            this.$message.error(res.msg);
            return;
          }
          this.$message.success('新增成功');
          this.$emit('cancelClose', true, 'add');
        });
      }
    },
    cancel() {
      if (this.id) {
        this.$emit('cancelClose', false, 'edit');
      } else {
        this.$emit('cancelClose', false, 'add');
      }
    }
  }
};
</script>
